package com.riant.test1.jaxb;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class TestDemo {
    public static void main(String[] args) {
        try {
            //第一步，创建一个SAX转换工厂
            SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
            //第二步，创建一个TransformerHandler实例
            TransformerHandler handler = factory.newTransformerHandler();
            //第三步：创建一个handler转换器
            Transformer transformer = handler.getTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");//换行
            transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");//设置字符集
            //第四步：创建一个Result实例连接到XML文件
            Result result = new StreamResult(new FileOutputStream(new File("E:\\sax.xml")));
            handler.setResult(result);
            // 创建一个属性实例, addAttribute(String uri, String localName, String qName, String type, String value)
            AttributesImpl attr = new AttributesImpl();
            // 打开doc对象
            handler.startDocument();
            // 创建元素: handler.startElement(uri, 命名空间, 元素名, 属性列表);//没有则填null
            handler.startElement("", "", "root", attr);

            // 每次创建节点前，先清空属性，放置一些属性错误
            attr.clear();
            // 设置属性
            attr.addAttribute("", "", "attr", "", "one");
            // 根节点第一个子节点person
            handler.startElement("", "", "person", attr);
            attr.clear();
            attr.addAttribute("", "", "attr", "", "one");
            // 第一个子节点的第一个子节点people
            handler.startElement("", "", "people", attr);
            handler.characters("one child people one".toCharArray(), 0, 14);
            handler.endElement("", "", "people");

            attr.clear();
            attr.addAttribute("", "", "attr", "", "two");
            // 第一个子节点的第二个子节点people
            handler.startElement("", "", "people", attr);
            handler.characters("one child people two".toCharArray(), 0, 14);
            handler.endElement("", "", "people");
            handler.endElement("", "", "person");
            // 每次创建节点前，先清空属性，放置一些属性错误
            attr.clear();
            // 设置属性
            attr.addAttribute("", "", "attr", "", "two");
            // 根节点第二个子节点person
            handler.startElement("", "", "person", attr);
            attr.clear();
            attr.addAttribute("", "", "attr", "", "one");
            // 第二个子节点的第一个子节点people
            handler.startElement("", "", "people", attr);
            handler.characters("two child people one".toCharArray(), 0, 14);
            handler.endElement("", "", "people");
            attr.clear();
            attr.addAttribute("", "", "attr", "", "two");
            // 第二个子节点的第二个子节点people
            handler.startElement("", "", "people", attr);
            handler.characters("two child people two".toCharArray(), 0, 14);
            handler.endElement("", "", "people");
            handler.endElement("", "", "person");
            // 结束hanlder.endElement("", "", 元素名)
            handler.endElement("", "", "root");
            // 关闭doc对象
            handler.endDocument();
            System.out.println("SAX CreateSAX success!");
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
